package com.wanlian.router.ui.integral;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smart.android.image.ImageLoader;
import com.smart.android.ui.tools.BaseListFragment;
import com.smart.android.ui.tools.MyNiuBAdapter;
import com.wanlian.router.R;
import com.wanlian.router.bean.IntegralTotal;
import com.wanlian.router.bean.TelecomRechargeCard;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/8/29.
 * Email: fvaryu@qq.com
 */
public class IntegralMallFragment extends BaseListFragment {
    private TelecomRechargeCardAdapter mTelecomRechargeCardAdapter;
    private List<TelecomRechargeCard> mTelecomRechargeCards;
    private int useAble;

    public static IntegralMallFragment newInstance() {
        return new IntegralMallFragment();
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        mTelecomRechargeCards = new ArrayList<>();
        setAdapter(mTelecomRechargeCardAdapter = new TelecomRechargeCardAdapter(getActivity(), mTelecomRechargeCards));

    }

    @Override
    protected void initData(View view) {
        super.initData(view);
    }


    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);
        if (!canToLoad(isRefresh)) {
            return;
        }
        NetWorkingClient.getInstance().queryIntegral(getActivity(),0, new INetCallBack<List<IntegralTotal> >() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<IntegralTotal>  integralTotals) {
                if (response.isSuccess()) {

                    if (integralTotals != null && integralTotals.size() > 0) {

                        IntegralTotal integralTotal = integralTotals.get(0);
                        useAble = (int) integralTotal.getDscore();

                       load(isRefresh);
                    }
                }else {
                    resetPull();
                    showToast(response.getMessage());
                }
            }
        });

    }

    private void load(final boolean isRefresh) {
        NetWorkingClient.getInstance().queryTelecomMall(getActivity(), getPageInfo(), new INetCallBack<List<TelecomRechargeCard>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<TelecomRechargeCard> telecomRechargeCards) {
                resetPull();
                if (response.isSuccess()) {
                    if (isRefresh) {
                        mTelecomRechargeCards.clear();
                    }
                    if (telecomRechargeCards != null) {
                        mTelecomRechargeCards.addAll(telecomRechargeCards);
                    }
                    mTelecomRechargeCardAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    class TelecomRechargeCardAdapter extends MyNiuBAdapter<TelecomRechargeCard> {


        public TelecomRechargeCardAdapter(Context context, List<TelecomRechargeCard> mTs) {
            super(context, mTs);
        }

        @Override
        public int layout() {
            return R.layout.layout_item_integral_mall;
        }

        @Override
        public MyViewHolder<TelecomRechargeCard> holder(View view) {
            return new MallHolder(view);
        }
    }

    class MallHolder extends MyNiuBAdapter.MyViewHolder<TelecomRechargeCard> {

        @BindView(R.id.image_logo)
        ImageView mImageViewLogo;
        @BindView(R.id.tv_name)
        TextView mName;
        @BindView(R.id.tv_price)
        TextView mPrice;
        @BindView(R.id.tv_exchange)
        TextView mExahange;

        public MallHolder(View view) {
            super(view);
        }

        @Override
        public void setData(int i, TelecomRechargeCard telecomRechargeCard) {

            ImageLoader.load(mImageViewLogo.getContext(),telecomRechargeCard.getImgPath(), mImageViewLogo);
            mExahange.setEnabled(useAble >= telecomRechargeCard.getScore());
            mName.setText(telecomRechargeCard.getName());
            mPrice.setText(String.format(Locale.getDefault(), "%d积分", telecomRechargeCard.getScore()));

            mExahange.setTag(telecomRechargeCard);
        }

        @OnClick(R.id.tv_exchange)
        void exchange(View view) {
            TelecomRechargeCard telecomRechargeCard = (TelecomRechargeCard) view.getTag();
            NetWorkingClient.getInstance().exchageTelecomCard(getActivity(), telecomRechargeCard, new INetCallBack() {
                @Override
                public void onFinish(ResponseData response, @Nullable Object o) {
                    if (response.isSuccess()) {
                        showToast("兑换成功");
                        loadData(true);
                    }else {
                        showToast(response.getMessage());
                    }
                }
            });

        }
    }

}
